package com.github.tj123.frame.controller;

import com.github.tj123.frame.common.page.PageRequest;
import com.github.tj123.frame.common.page.PageResponse;
import com.github.tj123.frame.pojo.dto.SAreaDto;
import com.github.tj123.frame.pojo.po.SAreaPo;
import com.github.tj123.frame.service.SAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/sarea")
public class SAreaController {

    @Autowired
    private SAreaService service;

    @PutMapping
    public void add(@Valid SAreaDto dto,BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BindException(result);
        }
        service.add(dto.toPo());
    }

    @DeleteMapping("/{id}")
    public void del(@PathVariable String id) throws Exception {
        service.del(id);
    }

    @PatchMapping
    public void edit(SAreaDto dto) throws Exception {
        if (dto.getAreaId() == null || dto.getAreaId().trim().equals("")) {
            throw new Exception("id 不能为空!");
        }
        service.edit(dto.toPo());
    }

    @GetMapping("/get/{id}")
    public SAreaPo get(@PathVariable String id) throws Exception {
        return service.get(id);
    }

    @GetMapping("/get2/{id}")
    public Map<String, Object> get2(@PathVariable String id) throws Exception {
        return service.get2(id);
    }

    @GetMapping
    public PageResponse<Map<String, Object>> list(@RequestParam Map<String, Object> map) throws Exception {
        return service.list(PageRequest.create(map));
    }

    @GetMapping("/2")
    public PageResponse<SAreaPo> list2(@RequestParam Map<String, Object> map) throws Exception {
        return service.list2(PageRequest.create(map));
    }

}
